package com.eptok.weixin.util;

import java.io.UnsupportedEncodingException;
import java.util.Map;
import java.util.Map.Entry;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

import org.apache.log4j.Logger;

import com.eptok.ys8583.util.ByteUtil;

import yspos.online.common.LogUtil;

public class AESUtil {
	
	private static final Logger log = Logger.getLogger(AESUtil.class);
	public static final String  ENC_KEY = "5F11FA4EA4E8FFC4C5D374F09B67E206";
    
    /**
     * 加密
     * 
     * @param content 需要加密的内容
     * @param password  加密密钥
     * @return
     */
    public static byte[] encrypt(byte[] content, String password) {
            try {          
            		SecretKeySpec key = new SecretKeySpec(ByteUtil.hexStringToBytes(password),"AES");
                    Cipher cipher = Cipher.getInstance("AES");// 创建密码器
                    cipher.init(Cipher.ENCRYPT_MODE, key);   // 初始化
                    byte[] result = cipher.doFinal(content);
            		LogUtil.APP.info("加密后数据：" + ByteUtil.bytesToHexString(result));
                    return result; // 加密
            } catch (Exception e) {
                    LogUtil.APP.error("加密数据出错", e);
            }
            return null;
    }
    
    
    /**解密
     * @param content  待解密内容
     * @param password 解密密钥
     * @return
     */
    public static byte[] decrypt(byte[] content, String password) {
            try {
                    SecretKeySpec key = new SecretKeySpec(ByteUtil.hexStringToBytes(password),"AES");
                    Cipher cipher = Cipher.getInstance("AES");// 创建密码器
                    cipher.init(Cipher.DECRYPT_MODE, key);// 初始化
                    byte[] result = cipher.doFinal(content);
            		LogUtil.APP.info("解密后数据：" + ByteUtil.bytesToHexString(result));
                    return result; // 解密
            } catch (Exception e) {
                LogUtil.APP.error("解密数据出错", e);
                e.printStackTrace();
            }
            return null;
    }
    
    public static String mapToEncrypt(Map<String,String> map) throws UnsupportedEncodingException
    {
    	StringBuffer sb = new StringBuffer();
    	for (Entry<String, String>  parms: map.entrySet()) {
    		sb.append(parms.getKey()).append("=").append(parms.getValue()).append("&");
		}
    	String para = sb.toString();
    	if(para != null && para.length()>0)
    	{
    		para = para.substring(0, para.length()-1);
    	}
    	log.info("加密前数据："+para);
    	byte[] ss = AESUtil.encrypt(para.getBytes("UTF-8"), AESUtil.ENC_KEY);
    	String encStr = ByteUtil.bytesToHexString(ss);
    	log.info("得到加密数据："+encStr);
    	return encStr;
    }
    
   /* public static void main(String[] args) {
    	
    	String oin = "mercName=深圳市壹网通测试&mercId=826440360128015&trmNo=A0147258";
    	log.info("加密前的数据："+oin);
    	byte[] ss = AESUtil.encrypt(oin.getBytes(), AESUtil.ENC_KEY);
    	String encStr = ByteUtil.bytesToHexString(ss);
    	log.info("得到加密数据："+encStr);
    	
    	
    	byte[] by2 = decrypt(ByteUtil.hexStringToBytes(encStr),"5F11FA4EA4E8FFC4C5D374F09B67E206");
    	//System.out.println(ByteUtil.bytesToHexString(by2));
    	log.info("解密后的数据："+new String(by2));
	}*/

}
